package com.ylx.jar.util;

import cn.hutool.core.lang.UUID;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ylx.jar.contast.TestConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试接口发送请求的根据
 * @author yanglixin
 */
public final class SendRequestUtil {

    /**
     * 获取管理端的token
     */
    public static String getToken() {
        String url = TestConstant.BASE_URL + "/auth-center-manage-api/getSuperToken";
        String post = HttpUtil.post(url, "{}");
        System.out.println("获取token响应: " + post);
        JSONObject jsonObject = JSON.parseObject(post);
        return  jsonObject.getString("token");
    }

    /**
     * 调用jar包demo发送请求
     * @param pram 请求参数
     * @param url 请求地址
     */
    public static void jarDemoSendRequest(JSONObject pram, String url) {
        HttpRequest post = HttpUtil.createPost(url);
        Map<String, String> header = new HashMap<>();
        header.put("Content-Type", "application/json");
        post.addHeaders(header);

        String transactionId = UUID.fastUUID().toString(true);
        System.out.println("transactionId: " + transactionId);
        pram.put("transactionId",transactionId);
        pram.put("timestamp",System.currentTimeMillis());

        System.out.println("请求地址: " + url);
        System.out.println("请求参数: " + pram.toJSONString());
        post.body(pram.toJSONString());
        HttpResponse response = post.execute();
        String responseBody = response.body();
        System.out.println("响应: " + responseBody);
    }
}
